﻿#ifndef TCPSERVERFRM_H
#define TCPSERVERFRM_H

#include <QWidget>

#include <net/tcpsession.h>

namespace Ui {
class TcpServerFrm;
}
namespace QNET_NAMESPACE {
class TcpServer;
}
QNET_USING_NAMESPACE

class QListWidgetItem;
/**
 * @brief The TcpServerFrm class tcp服务端测试。
 */
class TcpServerFrm : public QWidget,
        public ISessionListener
{
    Q_OBJECT

public:
    explicit TcpServerFrm(QWidget *parent = nullptr);
    ~TcpServerFrm() override;

    // ISessionListener interface
public:
    void onStateChanged(TcpSession *session, bool connected) override;
    void onError(QAbstractSocket::SocketError error) override;

private slots:
    void on_btnListen_clicked();

    void on_btnServerStop_clicked();

    void on_btnClear_clicked();
    void on_btnSend_clicked();

    void fulfil_slot(QSharedPointer<NetWriting> owner, int size);

    void on_listWidget_itemPressed(QListWidgetItem *item);

private:
    void logAppend(QString msg);
    void setServerStatus(bool enable);
    void onRecv(const NetReading *reading, const QByteArray &buffer);
private:
    Ui::TcpServerFrm *ui;
    QtSocket::TcpServer *m_server;
    TcpSession *m_curSession;
    friend class Reading;

};

#endif // TCPSERVERFRM_H
